home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / admin / xinetd.2 / xinetd / xinetd.2.1.7-linux.4 / libs / src / xlog / xlog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-25  |  4.6 KB  |  278 lines

  1. /*
  2.  * (c) Copyright 1992, 1993 by Panagiotis Tsirigotis
  3.  * All rights reserved.  The file named COPYRIGHT specifies the terms 
  4.  * and conditions for redistribution.
  5.  */
  6.  
  7. static char RCSid[] = "$Id: xlog.c,v 2.1 1993/05/06 07:39:39 panos Exp $" ;
  8. static char version[] = VERSION ;
  9.  
  10. #include <varargs.h>
  11.  
  12. #include "xlog.h"
  13. #include "impl.h"
  14.  
  15. char *malloc() ;
  16.  
  17. extern struct xlog_ops __xlog_filelog_ops ;
  18. #ifndef NO_SYSLOG
  19. extern struct xlog_ops __xlog_syslog_ops ;
  20. #endif
  21.  
  22. struct lookup_table
  23. {
  24.     struct xlog_ops    *ops ;
  25.     xlog_e                type ;
  26. } ;
  27.  
  28. static struct lookup_table ops_lookup_table[] =
  29.     {
  30.         { &__xlog_filelog_ops,                XLOG_FILELOG    },
  31. #ifndef NO_SYSLOG
  32.         { &__xlog_syslog_ops,                XLOG_SYSLOG        },
  33. #endif
  34.         { NULL }
  35.     } ;
  36.  
  37.  
  38. #define CALLBACK( xp, status )                                                                 \
  39.         if ( (xp)->xl_callback )                                                                 \
  40.             (*(xp)->xl_callback)( (xlog_h)(xp), status, (xp)->xl_callback_arg )
  41.  
  42.  
  43. PRIVATE void xlog_link() ;
  44. PRIVATE void xlog_unlink() ;
  45.  
  46.  
  47. PRIVATE struct xlog_ops *xlog_ops_lookup( type )
  48.     register xlog_e type ;
  49. {
  50.     register struct lookup_table *ltp ;
  51.  
  52.     for ( ltp = &ops_lookup_table[ 0 ] ; ltp->ops ; ltp++ )
  53.         if ( ltp->type == type )
  54.             break ;
  55.     return( ltp->ops ) ;
  56. }
  57.  
  58.  
  59.  
  60. /* VARARGS3 */
  61. xlog_h xlog_create( type, id, flags, va_alist )
  62.     xlog_e    type ;
  63.     char        *id ;
  64.     int        flags ;
  65.     va_dcl
  66. {
  67.     xlog_s                *xp ;
  68.     va_list                ap ;
  69.     struct xlog_ops    *xops ;
  70.     int                    status ;
  71.  
  72.     if ( ( xp = NEW( xlog_s ) ) == NULL )
  73.         return( NULL ) ;
  74.     
  75.     if ( id == NULL || ( xp->xl_id = __xlog_new_string( id ) ) == NULL )
  76.     {
  77.         FREE( xp ) ;
  78.         return( NULL ) ;
  79.     }
  80.  
  81.     xops = xlog_ops_lookup( type ) ;
  82.     
  83.     if ( xops != NULL )
  84.     {
  85.         va_start( ap ) ;
  86.         xp->xl_ops = xops ;
  87.         status = XL_INIT( xp, ap ) ;
  88.         va_end( ap ) ;
  89.  
  90.         if ( status == XLOG_ENOERROR )
  91.         {
  92.             xp->xl_flags = flags ;
  93.             xp->xl_type = type ;
  94.             xp->xl_clients = XLOG_NULL ;
  95.             xp->xl_use = XLOG_NULL ;
  96.             return( (xlog_h) xp ) ;
  97.         }
  98.     }
  99.  
  100.     free( xp->xl_id ) ;
  101.     FREE( xp ) ;
  102.     return( NULL ) ;
  103. }
  104.  
  105.  
  106.  
  107. PRIVATE void xlog_link( client, server )
  108.     xlog_s *client, *server ;
  109. {
  110.     client->xl_use = server ;
  111.     if ( server == NULL )
  112.         return ;
  113.  
  114.     if ( server->xl_clients == XLOG_NULL )
  115.     {
  116.         INIT_LINKS( client, xl_other_users ) ;
  117.         server->xl_clients = client ;
  118.     }
  119.     else
  120.         LINK( server, client, xl_other_users ) ;
  121. }
  122.  
  123.  
  124. PRIVATE void xlog_unlink( xp )
  125.     xlog_s *xp ;
  126. {
  127.     xlog_s *server = xp->xl_use ;
  128.  
  129.     /*
  130.      * Step 1: remove from server chain
  131.      */
  132.     if ( server != XLOG_NULL )
  133.     {
  134.         if ( server->xl_clients == xp )
  135.             if ( NEXT( xp, xl_other_users ) == xp )
  136.                 server->xl_clients = XLOG_NULL ;
  137.             else
  138.                 server->xl_clients = NEXT( xp, xl_other_users ) ;
  139.         else
  140.             UNLINK( xp, xl_other_users ) ;
  141.     }
  142.  
  143.     /*
  144.      * Step 2: If we have users, clear their link to us.
  145.      */
  146.     if ( xp->xl_clients != NULL )
  147.     {
  148.         xlog_s *xp2 = xp->xl_clients ;
  149.  
  150.         do
  151.         {
  152.             xp2->xl_use = XLOG_NULL ;
  153.             xp2 = NEXT( xp2, xl_other_users ) ;
  154.         }
  155.         while ( xp2 != xp->xl_clients ) ;
  156.     }
  157. }
  158.  
  159.  
  160. PRIVATE void xlog_flags( xp, cmd, ap )
  161.     xlog_s        *xp ;
  162.     xlog_cmd_e    cmd ;
  163.     va_list        ap ;
  164. {
  165.     int    flag            = va_arg( ap, int ) ;
  166.     int    old_value    = ( ( xp->xl_flags & flag ) != 0 ) ;
  167.     int    *valp            = va_arg( ap, int * ) ;
  168.  
  169.     if ( cmd == XLOG_SETFLAG )
  170.         if ( *valp )
  171.             xp->xl_flags |= flag ;
  172.         else
  173.             xp->xl_flags &= ~flag ;
  174.     *valp = old_value ;
  175. }
  176.  
  177.  
  178. void xlog_destroy( xlog )
  179.     xlog_h xlog ;
  180. {
  181.     xlog_s *xp = XP( xlog ) ;
  182.  
  183.     xlog_unlink( xp ) ;
  184.     XL_FINI( xp ) ;
  185.     free( xp->xl_id ) ;
  186.     FREE( xp ) ;
  187. }
  188.  
  189.  
  190. /* VARARGS4 */
  191. void xlog_write( xlog, buf, len, flags, va_alist )
  192.     xlog_h    xlog ;
  193.     char        buf[] ;
  194.     int        len ;
  195.     int        flags ;
  196.     va_dcl
  197. {
  198.     xlog_s    *xp = XP( xlog ) ;
  199.     va_list    ap ;
  200.     int        status ;
  201.  
  202.     va_start( ap ) ;
  203.     status = XL_WRITE( xp, buf, len, flags, ap ) ;
  204.     va_end( ap ) ;
  205.  
  206.     if ( status != XLOG_ENOERROR )
  207.     {
  208.         CALLBACK( xp, status ) ;
  209.     }
  210. }
  211.  
  212.  
  213. /* VARARGS2 */
  214. int xlog_control( xlog, cmd, va_alist )
  215.     xlog_h        xlog ;
  216.     xlog_cmd_e    cmd ;
  217.     va_dcl
  218. {
  219.     va_list    ap ;
  220.     xlog_s    *xp        = XP( xlog ) ;
  221.     int        status    = XLOG_ENOERROR ;
  222.  
  223.     va_start( ap ) ;
  224.  
  225.     switch ( cmd )
  226.     {
  227.         case XLOG_LINK:
  228.             xlog_unlink( xp ) ;
  229.             xlog_link( xp, va_arg( ap, xlog_s * ) ) ;
  230.             xp->xl_callback_arg = va_arg( ap, void * ) ;
  231.             break ;
  232.         
  233.         case XLOG_CALLBACK:
  234.             xp->xl_callback = va_arg( ap, voidfunc ) ;
  235.             break ;
  236.             
  237.         case XLOG_GETFLAG:
  238.         case XLOG_SETFLAG:
  239.             xlog_flags( xp, cmd, ap ) ;
  240.             break ;
  241.  
  242.         default:
  243.             status = XL_CONTROL( xp, cmd, ap ) ;
  244.     }
  245.  
  246.     va_end( ap ) ;
  247.  
  248.     return( status ) ;
  249. }
  250.  
  251.  
  252. int xlog_parms( type, va_alist )
  253.     xlog_e type ;
  254.     va_dcl
  255. {
  256.     va_list    ap ;
  257.     int        status ;
  258.  
  259.     va_start( ap ) ;
  260.     switch ( type )
  261.     {
  262. #ifndef NO_SYSLOG
  263.         case XLOG_SYSLOG:
  264.             status = (*__xlog_syslog_ops.parms)( ap ) ;
  265.             break ;
  266. #endif
  267.         case XLOG_FILELOG:
  268.             status = (*__xlog_filelog_ops.parms)( ap ) ;
  269.             break ;
  270.         
  271.         default:
  272.             status = XLOG_ENOERROR ;
  273.     }
  274.     va_end( ap ) ;
  275.     return( status ) ;
  276. }
  277.  
  278.